package com.huage.courseplatform.satoken.controller;

import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.model.param.FormLoginPARAM;
import com.huage.courseplatform.satoken.model.param.WxLoginPARAM;
import com.huage.courseplatform.satoken.model.vo.LoginVO;
import com.huage.courseplatform.satoken.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bind.annotation.Empty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping(value = "/login")
@Api(tags = "登录接口")
@Slf4j
public class LoginController {
    @Resource
    private LoginService loginService;

    @ApiOperation(value = "表单登录")
    @PostMapping(value = "/formLogin")
    public ResultUtils<LoginVO> formLogin(HttpServletRequest request, @RequestBody @Validated FormLoginPARAM formLoginParam) {
        String ip = request.getRemoteAddr();
        return ResultUtils.success(loginService.formLogin(formLoginParam,ip));
    }

    @ApiOperation(value = "小程序core登录(自动注册)")
    @PostMapping(value = "/wxLogin/{code}")
    public ResultUtils<LoginVO> wxLogin(HttpServletRequest request,@NotEmpty(message = "code不能为空") @PathVariable String code) {
        String ip = request.getRemoteAddr();
        return ResultUtils.success(loginService.wxLogin(code,ip));
    }


    @ApiOperation(value = "根据Token退出登录")
    @PostMapping(value = "/loginOut/{token}")
    public ResultUtils<String> loginOutByToken(@NotEmpty(message = "token不能为空") @PathVariable String token) {
        loginService.loginOutByToken(token);
        return ResultUtils.success("退出成功");
    }

    @ApiOperation(value = "根据用户ID退出登录")
    @PostMapping(value = "/loginOutById/{id}")
    public ResultUtils<String> loginOutById(@NotEmpty(message = "id不能为空") @PathVariable String id) {
        loginService.loginOutById(id);
        return ResultUtils.success("退出成功");
    }


}
